/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.PriorityCodeDAO;
import exatrez.manager.PriorityManager;
import exatrez.model.InfoAudit;
import exatrez.model.PriorityCode;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminPriority")
@AuthRequired
public class PriorityAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(PriorityAction.class);
    private PriorityCode priority;
    private List<PriorityCode> listPriority;
    @Inject
    private PriorityManager priorityManager;
    @Inject
    private PriorityCodeDAO priorityCodeDAO;

    @DefaultHandler
    public Resolution add_priority() {
        priority = new PriorityCode();
        listPriority = priorityManager.findAll();
        return new JSP("admin/priority.jsp");
    }

    public Resolution savePriority() {
        PriorityCode p = priorityManager.findPriorityCode(priority.getPriorityName());
        LOG.debug("masuk");
        if (p == null) {
            User user = getContext().getCurrentUser();
            InfoAudit ia = new InfoAudit();
            ia.setCreateDate(new Date());
            ia.setCreateId(user);
            priority.setInfoAudit(ia);
            priorityManager.saveOrUpdate(priority);
            addSimpleMessage("Success");
            LOG.debug("finish");
        } else {
            addSimpleError("Duplicate Priority!");
        }
        return add_priority();
    }

    public Resolution deletePriority() {
        String priorityCodeId = getContext().getRequest().getParameter("priorityCodeId");
        if (priorityCodeId != null) {
            priority = priorityCodeDAO.findById(Integer.parseInt(priorityCodeId));
            if (priority != null) {
                priorityManager.deletePriority(priority);
                addSimpleMessage("Success Delete");
            }
        }
        return new RedirectResolution(PriorityAction.class, "add_priority");
    }

    public Resolution editPriority() {
        String priorityCodeId = getContext().getRequest().getParameter("priorityCodeId");
        if (priorityCodeId != null) {
            priority = priorityCodeDAO.findById(Integer.parseInt(priorityCodeId));
        }
        return new JSP("admin/edit_priority.jsp").addParameter("popup", true);
    }

    public Resolution saveEditPriority() {
        String priorityCodeId = getContext().getRequest().getParameter("priority.priorityCodeId");
        if (priorityCodeId != null) {
            User user = getContext().getCurrentUser();
            PriorityCode p = priorityCodeDAO.findById(Integer.parseInt(priorityCodeId));
            if (!priority.getPriorityName().equals(p.getPriorityName())) {
                PriorityCode pc = priorityManager.findNotPriorityName(String.valueOf(p.getPriorityCodeId()), priority.getPriorityName());
                if (pc != null) {
                     addSimpleError("Duplicate Priority Name!");
                    return add_priority();
                } else {
                    p.setPriorityName(priority.getPriorityName());
                }
            }
            InfoAudit ia = p.getInfoAudit();

            p.setPriorityDay(priority.getPriorityDay());
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            p.setInfoAudit(ia);
            priorityManager.saveOrUpdate(p);
            addSimpleMessage("Success Update");
        }
        return new RedirectResolution(PriorityAction.class, "add_priority");
    }

    public PriorityManager getPriorityManager() {
        return priorityManager;
    }

    public void setPriorityManager(PriorityManager priorityManager) {
        this.priorityManager = priorityManager;
    }

    public PriorityCode getPriority() {
        return priority;
    }

    public void setPriority(PriorityCode priority) {
        this.priority = priority;
    }

    public List<PriorityCode> getListPriority() {
        return listPriority;
    }

    public void setListPriority(List<PriorityCode> listPriority) {
        this.listPriority = listPriority;
    }

    public PriorityCodeDAO getPriorityCodeDAO() {
        return priorityCodeDAO;
    }

    public void setPriorityCodeDAO(PriorityCodeDAO priorityCodeDAO) {
        this.priorityCodeDAO = priorityCodeDAO;
    }
}
